package com.lxh.cptp.config;

import com.google.common.collect.Maps;
import com.lxh.cptp.handler.*;
import com.lxh.cptp.tp.WxCpTpRedisConfigImpl;
import me.chanjar.weixin.cp.api.WxCpTpService;
import me.chanjar.weixin.cp.api.impl.WxCpTpServiceImpl;
import me.chanjar.weixin.cp.config.impl.WxCpTpDefaultConfigImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.PostConstruct;
import java.util.Map;

/**
 * created by lanxinghua@2dfire.com on 2020/2/23
 * 支持一个第三方
 */
@Configuration
@EnableConfigurationProperties(WxCpTpProperties.class)
public class WxCpTpConfiguration {
    private static final Logger logger = LoggerFactory.getLogger(WxCpTpConfiguration.class);
    public static String SUIT_ID = "ww141f5c1b0bfe0bc2";

    private static Map<String/*suitId*/, Map<String/*key*/, AbstractHandler>> routers = Maps.newHashMap();
    private static Map<String/*suitId*/, WxCpTpService> cpTpServices = Maps.newHashMap();

    @Autowired
    private WxCpTpProperties properties;

    // 更新2020.2.25 加入redis
    @Autowired
    private JedisPool jedisPool;

    public static WxCpTpService getCpTpService(String suitId) {
        return cpTpServices.get(suitId);
    }

    public static Map<String, AbstractHandler> getHandlerMap(){
        return routers.get(SUIT_ID);
    }

    public static AbstractHandler getHandler(String key){
        return routers.get(SUIT_ID).get(key);
    }

    /**
     * 初始化服务
     */
    @PostConstruct
    public void initServices() {
        WxCpTpService tpService = new WxCpTpServiceImpl();
        WxCpTpDefaultConfigImpl configStorage = new WxCpTpDefaultConfigImpl();
        // 加入redis缓存
        if (redisIsOk()){
            configStorage = new WxCpTpRedisConfigImpl(jedisPool);
        }
        configStorage.setSuiteId(properties.getSuiteId());
        configStorage.setAesKey(properties.getAesKey());
        configStorage.setToken(properties.getToken());
        configStorage.setSuiteSecret(properties.getSecret());
        configStorage.setCorpId(properties.getCorpId());

        tpService.setWxCpTpConfigStorage(configStorage);
        cpTpServices.put(properties.getSuiteId(), tpService);
        routers.put(properties.getSuiteId(), initHandler());
    }


    /**
     *
     * @return
     */
    private Map<String, AbstractHandler> initHandler(){
        Map<String, AbstractHandler> handlerMap = Maps.newHashMap();
        handlerMap.put("log", new LogHandler());
        handlerMap.put("msg", new MsgHandler());
        return handlerMap;
    }


    /**
     * redis是否可用
     * @return
     */
    private boolean redisIsOk(){
        try {
            Jedis jedis = jedisPool.getResource();
            jedis.ping();
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
    }
}
